package com.Prot2.Logger;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.provider.Settings;

import com.Prot2.Globals;

public class ToggleWorker {
	
	public static void toggle(Context c){
		
		Globals.mWifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
		
		switch(Globals.togglingPreferences[Globals.currentlyEstablishedLabel]){
		case 0: //both off
			setGPS(false,c);
			setWifi(false);
			break;
		case 1: //only GPS
			setGPS(true,c);
			setWifi(false);
			break;
		case 2: //only Wifi
			setGPS(false,c);
			setWifi(true);
			break;
		case 3: //both on
			setGPS(true,c);
			setWifi(true);
			break;
		default: //shouldn't happen
			break;
		}
	}
	
	
	private static void setGPS(boolean setTo, Context c){
		if(setTo){
			
		}else{
			System.out.println("GPS is currently: "+Globals.gpsWorker.getRead(","));
			System.out.println("trying to disable GPS");
		    String provider = Settings.Secure.getString(c.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		    if(provider.contains("gps")){ //if gps is enabled
		    	System.out.println("found that GPS is currently running");
		    	
	////APPROACH FOUND BY ULF	    	
//		        final Intent poke = new Intent();
//		        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
//		        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
//		        poke.setData(Uri.parse("3")); 
//		        c.sendBroadcast(poke);
		   
   ////APPROACH TWO
		    	Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
		    	intent.putExtra("enabled", false);
		    	c.sendBroadcast(intent);
		    	
		        System.out.println("GPS is now: "+Globals.gpsWorker.getRead(","));
		    }
		}
	}
	
	private static void setWifi(boolean setTo){
		Globals.mWifiManager.setWifiEnabled(setTo);
		System.out.println("WIFI is now: " + Globals.mWifiManager.isWifiEnabled());
	}

}
